package com.hlab.fabrevealmenu.view;

import com.hlab.fabrevealmenu.ResourceTable;
import com.hlab.fabrevealmenu.helper.Direction;
import com.hlab.fabrevealmenu.model.FABMenuItem;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.VectorElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

import java.util.List;

/**
 * FABMenuProvider
 *
 * @since 2021-05-14
 */
public class FABMenuProvider extends BaseItemProvider {
    private static final int INT_DEFAULT = -1;
    private final FABRevealMenu parent;
    private List<FABMenuItem> mItems;
    private int rowLayoutResId;
    private boolean isShowTitle;
    private boolean isShowIcon;
    private int titleTextColor;
    private int titleDisabledTextColor;
    private Direction direction;
    private Font mMenuTitleTypeface;
    private boolean isCircularShape;

    /**
     * FABMenuProvider
     *
     * @param parent parent
     * @param items items
     * @param rowLayoutResId rowLayoutResId
     * @param isCircularShape isCircularShape
     * @param titleTextColor titleTextColor
     * @param titleDisabledTextColor titleDisabledTextColor
     * @param isShowTitle isShowTitle
     * @param isShowIcon isShowIcon
     * @param direction direction
     */
    FABMenuProvider(FABRevealMenu parent, List<FABMenuItem> items,
                    int rowLayoutResId, boolean isCircularShape, int titleTextColor, int titleDisabledTextColor,
                    boolean isShowTitle, boolean isShowIcon, Direction direction) {
        this.parent = parent;
        this.mItems = items;
        this.rowLayoutResId = rowLayoutResId;
        this.isCircularShape = isCircularShape;
        this.isShowTitle = isShowTitle;
        this.isShowIcon = isShowIcon;
        this.titleTextColor = titleTextColor;
        this.titleDisabledTextColor = titleDisabledTextColor;
        this.direction = direction;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(parent.getContext()).parse(rowLayoutResId, componentContainer, false);
        } else {
            cpt = component;
        }
        FABMenuItem fabMenuItem = mItems.get(i);
        DependentLayout viewParent = (DependentLayout) cpt.findComponentById(ResourceTable.Id_view_parent);
        viewParent.setTag(mItems.get(i).getId());

        Text tvTitle = (Text) cpt.findComponentById(ResourceTable.Id_txt_title_menu_item);
        tvTitle.setText(fabMenuItem.getTitle());
        tvTitle.setVisibility(isShowTitle ? Component.VISIBLE : Component.HIDE);
        tvTitle.setTextColor(new Color(titleTextColor));
        if (mMenuTitleTypeface != null) {
            tvTitle.setFont(mMenuTitleTypeface);
        }
        Image imgIcon = (Image) cpt.findComponentById(ResourceTable.Id_img_menu_item);
        imgIcon.setVisibility(isShowIcon ? Component.VISIBLE : Component.HIDE);
        if (mItems.get(i).getIconPixelMap() != null) {
            imgIcon.setPixelMap(mItems.get(i).getIconPixelMap());
        } else if (mItems.get(i).getIconResId() != INT_DEFAULT) {
            VectorElement vectorElement = new VectorElement(parent.getContext(), mItems.get(i).getIconResId());
            imgIcon.setImageElement(vectorElement);
        }

        viewParent.setClickedListener(component1 -> {
            if (mItems.get(i).isEnabled() && !parent.getAnimationHelper().isShowMenuAni()) {
                parent.closeMenu();
                parent.getMenuSelectedListener().onMenuItemSelected(component1, mItems.get(i).getId());
            }
        });
        viewParent.invalidate();
        cpt.setEnabled(mItems.get(i).isEnabled());
        tvTitle.setEnabled(mItems.get(i).isEnabled());

        return cpt;
    }

    FABMenuItem getItemByIndex(int index) {
        if (index >= 0 && index < mItems.size()) {
            return mItems.get(index);
        }
        return null;
    }

    FABMenuItem getItemById(int id) {
        for (FABMenuItem mItem : mItems) {
            if (mItem.getId() == id) {
                return mItem;
            }
        }
        return null;
    }

    void notifyItemChangedById(int id) {
        for (int i = 0; i < mItems.size(); i++) {
            if (mItems.get(i).getId() == id) {
                notifyDataSetItemChanged(i);
                return;
            }
        }
    }

    public boolean isShowTitle() {
        return isShowTitle;
    }

    public void setShowTitle(boolean isShow) {
        this.isShowTitle = isShow;
    }

    public boolean isShowIcon() {
        return isShowIcon;
    }

    public void setShowIcon(boolean isShow) {
        this.isShowIcon = isShow;
    }

    public int getTitleTextColor() {
        return titleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.titleTextColor = titleTextColor;
    }

    void setTitleDisabledTextColor(int titleDisabledTextColor) {
        this.titleDisabledTextColor = titleDisabledTextColor;
    }

    public Direction getDirection() {
        return direction;
    }

    void setDirection(Direction direction) {
        this.direction = direction;
    }

    void setMenuTitleTypeface(Font menuTitleTypeface) {
        this.mMenuTitleTypeface = menuTitleTypeface;
    }
}
